package com.wdc.贪心;

@SuppressWarnings("all")
public class E09Leetcode135 {
//    public int candy(int[] ratings) {
//        int res = ratings.length;
//        for (int i = 1; i < ratings.length; i++) {
//            if (ratings[i - 1] == ratings[i]) {
//                continue;
//            }else {
//                res++;
//            }
//        }
//        return res;
//    }

    public int candy(int[] ratings) {
        int len = ratings.length;
        int[] candyVec = new int[len];
        candyVec[0] = 1;
        for (int i = 1; i < len; i++) {
            candyVec[i] = (ratings[i] > ratings[i - 1]) ? candyVec[i - 1] + 1 : 1;
        }

        for (int i = len - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                candyVec[i] = Math.max(candyVec[i], candyVec[i + 1] + 1);
            }
        }

        int ans = 0;
        for (int num : candyVec) {
            ans += num;
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] ratings = {1, 3, 2, 2, 1};
        E09Leetcode135 e09Leetcode135 = new E09Leetcode135();
        System.out.println(e09Leetcode135.candy(ratings));
    }

}
